package com.wngbms.grpc.demo;

import com.wngbms.grpc.hello.HelloReq;
import com.wngbms.grpc.hello.HelloResp;
import com.wngbms.grpc.hello.HelloServiceGrpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;

import java.util.concurrent.TimeUnit;

public class DemoClient {
    ManagedChannel managedChannel;
    HelloServiceGrpc.HelloServiceBlockingStub blockingStub;

    public DemoClient() {
        ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 8090).usePlaintext().build();
        managedChannel = channel;
        blockingStub = HelloServiceGrpc.newBlockingStub(channel);
    }

    public void shutdown() throws InterruptedException {
        managedChannel.shutdown().awaitTermination(6, TimeUnit.SECONDS);
    }

    public void echo() {
        HelloReq req = HelloReq.newBuilder().setName("Tim").build();
        HelloResp say = blockingStub.say(req);
        System.out.println(say.getMessage());
    }

    public static void main(String[] args) throws InterruptedException {
        DemoClient client = new DemoClient();
        client.echo();
        client.shutdown();
    }

}
